package chu.dejian.widget.chartView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.widget.FrameLayout;

import org.achartengine.GraphicalView;
import org.achartengine.chart.XYChart;

import java.util.ArrayList;
import java.util.List;

import com.sam.blemodule.R;
import chu.dejian.entity.RecordEntity;
import com.sam.utils.L;

public class BPChartView extends FrameLayout{
    private BaseBPChartUtils chartCreator;
    private Context context;
    private GraphicalView bpChart;
    private List<RecordEntity> recordEntityList;
    private int lineColor = Color.parseColor("#929292");
    private boolean showPoint = true;
    private BaseBPChartUtils.CHART_STYLE chartStyle;
//    private int firstLabelTextSize,secondLabelTextSize;
//    private int chartMarginTop,chartMarginLeft,chartMarginBottom,chartMarginRight;
//    private int xLabelsPadding,yLabelsPadding;

    public enum DISPLAY_TYPE { // 图表显示为每天的，每月的，每三月的
        DAY(0),MONTH(1),THREE_MONTH(2),DIALOG(3);
        private int value;
        DISPLAY_TYPE(int value) {
            this.value = value;
        }
        public static DISPLAY_TYPE fromInt(int value){
            switch (value){
                case 0: return DAY;
                case 1: return MONTH;
                case 2: return THREE_MONTH;
                case 3: return DIALOG;
                default:return DAY;
            }
        }
    }


    public BPChartView(Context context) {
        this(context,null);
    }

    public BPChartView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BPChartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.widget_chart, this);
        getAttrs(context,attrs);
    }

    protected void getAttrs(Context context, AttributeSet attrs){
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.BPChartView);
        showPoint = ta.getBoolean(R.styleable.BPChartView_showPoint,showPoint);
        lineColor = ta.getInt(R.styleable.BPChartView_lineColor, lineColor);
//        firstLabelTextSize = ta.getInt(R.styleable.BPChartView_firstLabelTextSize, lineColor);
//        secondLabelTextSize = ta.getInt(R.styleable.BPChartView_secondLabelTextSize, lineColor);
//        chartMarginTop = ta.getInt(R.styleable.BPChartView_chartMarginTop, lineColor);
//        chartMarginLeft = ta.getInt(R.styleable.BPChartView_chartMarginLeft, lineColor);
//        chartMarginBottom = ta.getInt(R.styleable.BPChartView_chartMarginBottom, lineColor);
//        chartMarginRight = ta.getInt(R.styleable.BPChartView_chartMarginRight, lineColor);
//        xLabelsPadding = ta.getInt(R.styleable.BPChartView_xLabelsPadding, lineColor);
//        yLabelsPadding = ta.getInt(R.styleable.BPChartView_yLabelsPadding, lineColor);
        chartStyle = BaseBPChartUtils.CHART_STYLE.fromInt(ta.getInt(R.styleable.BPChartView_chartStyle,0));
        ta.recycle();
    }

    public void setLineColor(int color){
        this.lineColor = color;
    }

    public void setShowPoint(boolean showPoint){
        this.showPoint = showPoint;
    }

    public void setChartStyle(BaseBPChartUtils.CHART_STYLE chartStyle){
        this.chartStyle = chartStyle;
    }

//    public void setRecordEntityLists(List<List<RecordEntity>> recordEntityLists,DISPLAY_TYPE display_type,SHOW_DAY_REACORD_WAY whichRecord){
//        List<RecordEntity> recordEntityList = new ArrayList<>();
//        List<RecordEntity> currentRecordList = null;
//        for(int i = 0; i < recordEntityLists.size(); i++){
//            currentRecordList = recordEntityLists.get(i);
//            if(currentRecordList == null || currentRecordList.size() == 0) continue;
//            if(whichRecord == SHOW_DAY_REACORD_WAY.FIRST){
//            recordEntityList.add(currentRecordList.get(0));
//            } else if(whichRecord == SHOW_DAY_REACORD_WAY.LAST){
//                recordEntityList.add(currentRecordList.get(currentRecordList.size() - 1));
//            }
//        }
//        setRecordEntityList(recordEntityList,display_type);
//    }

    public void setRecordEntityList(List<RecordEntity> recordEntityList,DISPLAY_TYPE displayType){
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
        FrameLayout chartLayout = (FrameLayout) findViewById(R.id.chart_layout);

        this.recordEntityList = recordEntityList;
        if(displayType == DISPLAY_TYPE.DAY){
            chartCreator = new DayBPChartUtils(context, lineColor,showPoint);
        } else if(displayType == DISPLAY_TYPE.MONTH){
            chartCreator = new MonthBPChartUtils(context, lineColor,showPoint);
        } else if(displayType == DISPLAY_TYPE.THREE_MONTH){
            chartCreator = new ThreeMonthBPChartUtils(context, lineColor,showPoint);
        } else if(displayType == DISPLAY_TYPE.DIALOG){
            chartCreator = new DialogBPChartUtils(context, lineColor,showPoint);
        }
        chartCreator.setRecordEntityList(recordEntityList);
        bpChart = chartCreator.createChartView(chartStyle);
        bpChart.setLayoutParams(lp);
        chartLayout.removeAllViews();
        chartLayout.addView(bpChart);

        chartCreator.setOnPointClickedListener(onPointClickListener);
    }

    public void focuseNextRecord(){
        bpChart.focusedNextItem();
    }

    public void focusePreviousItem(){
        bpChart.focusePreviousItem();
    }

    private BaseBPChartUtils.OnPointClickListener onPointClickListener = new BaseBPChartUtils.OnPointClickListener() {
        @Override
        public void onPointClicked(double pointX, double pointY) {
            L.d("点击的点为：(" + pointX + "," + pointY + ")");
            if(recordPointClickListener != null && recordEntityList!= null && pointX >= 0 && pointX < recordEntityList.size())
                recordPointClickListener.onRecordPointClicked(recordEntityList.get((int)pointX),(int)pointX,(int)pointY);
        }
    };

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        getParent().requestDisallowInterceptTouchEvent(true); // 优先让本View来处理事件
        return super.dispatchTouchEvent(ev);
    }

    public List<RecordEntity> getShownRecordList(){return chartCreator.getShownRecordList();}
    public List<List<RecordEntity>> getGroupedRecordLists(){return chartCreator.getGroupedRecordLists();}

    public void setOnChartItemClickListener(XYChart.ChartItemClickListener chartItemClickListener){
        if(bpChart == null) throw new NullPointerException("必须先调用setRecordEntityList设置数据源，才能绑定点击事件");
        bpChart.setOnChartItemClickListener(chartItemClickListener);
    }

    RecordPointClickListener recordPointClickListener;
    public void setRecordPointClickListener(RecordPointClickListener recordPointClickListener){
        this.recordPointClickListener = recordPointClickListener;
    }
    public interface RecordPointClickListener{
        void onRecordPointClicked(RecordEntity recordEntity,int pointIndex,int pointValue);
    }



}
